package com.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.entity.Tag;
import com.service.TagService;



@Controller @RequestMapping("/tag")
public class TagController {
	
	@Resource
	TagService tagService;
	
	@RequestMapping("")
	public String tag(ModelMap m) {
		List<Tag> listCountTag = tagService.listCountTag();
		
		m.put("listCountTag", listCountTag);
		return "Tag/tag-list";
	}
	
	@RequestMapping("/add")
	public String addTag(HttpServletRequest request) {
		Tag tag = new Tag();
		tag.setTagName(request.getParameter("tagName"));
		tag.setTagDescription(request.getParameter("tagDescription"));
		tagService.addTag(tag);
		
		return "redirect:../tag";
	}
	
	@RequestMapping("/delete/{id}")
	public void deleteTag(@PathVariable("id") Integer id) {
		tagService.deleteTag(id);
		
	}
	
	@RequestMapping(value="/update/{id}",method=RequestMethod.GET)
	public String gotoUpdatePage(@PathVariable("id") Integer id, ModelMap m) {
		Tag tag = tagService.getTagsById(id);
		List<Tag> taglist= tagService.listCountTag();
		
		m.put("tag", tag);
		m.put("taglist", taglist);
		return "Tag/tag-update";
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public String updateTag(HttpServletRequest request) {
		Tag tag = new Tag();
		tag.setTagId(Integer.parseInt(request.getParameter("tagId")));
		tag.setTagName(request.getParameter("tagName"));
		tag.setTagDescription(request.getParameter("tagDescription"));
		
		tagService.deleteTag(Integer.parseInt(request.getParameter("tagId")));
		tagService.addTag(tag);
		
		return "redirect:../tag";
	}
}
